﻿using DynamicExpresso;
using EF6Query.Models.Req;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF6Query.Models.Extend
{
    public static class DynamicExtend
    {
        public static IQueryable<T> DynamicFilter<T>(this IQueryable<T> queryable, DynamicExpressionRequest item)
        {
            var interpreter = new Interpreter();
            Func<T, bool> productDynamic = interpreter.ParseAsDelegate<Func<T, bool>>(item.Expression, item.TableName);
            return queryable.Where(productDynamic).AsQueryable();
        }


        public static bool DynamicFilter<TSource>(this TSource source, DynamicExpressionRequest item)
        {
            var interpreter = new Interpreter();
            Func<TSource, bool> productDynamic = interpreter.ParseAsDelegate<Func<TSource, bool>>(item.Expression, item.TableName);
            return productDynamic(source);
        }

        //public static Func<TSource, bool> DynamicFilter<TSource>(this TSource source, DynamicExpressionRequest item)
        //{
        //    var interpreter = new Interpreter();
        //    Func<TSource, bool> productDynamic = interpreter.ParseAsDelegate<Func<TSource, bool>>(item.Expression, item.TableName);
        //    return productDynamic;
        //}
    }
}
